<?php namespace Common\Model;
use Hdphp\Model\Model;
class sc_user extends Model{
	
	//指定表
	protected $table='sc_user';
	
	//自动完成
	protected $auto=[
	     ['password','md5','function',3,3]
	];
	
	
	//定义制动验证
	protected $validate=[
	     ['account','required','账号必填',3,3],
	     ['account','regexp:/^\w{6,20}$/','账号由6到20位数字字母下划线组成',3,3],
	     ['username','required','昵称必填',3,3],
	     ['password','required','密码必填',3,3],
	     ['password','regexp:/^\w{6,20}$/','密码由6到20位数字字母下划线组成',3,3],
	     ['password2','required','确认密码必填',3,3],
		 ['password2','confirm:password','两次密码不一致',3,3],
		 ['email','required','邮箱必填',3,3],
		 ['email','email','邮箱格式不正确',3,3],
		 ['telphone','required','手机号必须填写',3,3],
		 ['telphone','phone','手机号格式不正确',3,3],
		 ['code','required','验证码必填',3,3],
		 ['code','_code','验证码不正确',3,3],
		 ['account','unique','用户名已经存在',3,3]
		 
	];
	
	
	
	//注册
	public function store(){
		
		if(!$this->create()) return false;
		
		$this->add();
		return true;
	}
	
	
	
	//登录
	public function checkLogin(){
	    
		//验证验证码是否正确
		$code=Q('post.code');
		if(strtolower($code)!=strtolower($_SESSION['code'])){
			$this->error='验证码错误';
			return false;
		}
		
		//验证账号密码
		$account=Q('post.account');
		$password=md5(Q('post.password'));
        
		$data=$this->where("account='{$account}' AND password='{$password}'")->find();
		
		if(!$data){
			$this->error='用户名或密码错误！';
			return false;
		}
		
		$_SESSION['info']=[
		    'uid'=>$data['uid'],
		    'username'=>$data['username']
		];
		return true;
	}
	
	
	//验证验证码
	public function _code(){
	    $code=Q('post.code');
		if(strtolower($code)!=strtolower($_SESSION['code'])){
			return false;
		}
		return true;
	}
	
	
}
